package edu.bit.water.service;

import edu.bit.water.dao.NewCleanProductDao;
import edu.bit.water.entity.MetaCleanProduct;
import edu.bit.water.entity.NewCleanProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 清洁生产的业务层
 * @author HJS
 *
 */
@Service
public class NewCleanProductService {

    @Autowired
    private NewCleanProductDao newCleanProductDao;


    /**
     * clean_product的记录
     * @return clean_product中的所有记录
     */
    public List<NewCleanProduct> select(){
        List<NewCleanProduct> list = newCleanProductDao.select();
        return list;
    }

    /**
     * 插入记录
     * @param newCleanProduct
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> insert(NewCleanProduct newCleanProduct) {
        Map<String, String> modelMap = new HashMap<>();
        int n = newCleanProductDao.insert(newCleanProduct);
        if (n == 1)
            modelMap.put("status", "success");
        else
            modelMap.put("status", "failed");
        return modelMap;
    }

    /**
     * 更新记录
     * @param newCleanProduct
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> update(NewCleanProduct newCleanProduct) {
        Map<String, String> modelMap = new HashMap<>();
        int n = newCleanProductDao.update(newCleanProduct);
        if (n == 1)
            modelMap.put("status", "success");
        else
            modelMap.put("status", "failed");
        return modelMap;
    }

    /**
     * 删除记录
     * @param ids 删除记录的标识符
     * @return 成功返回1,失败返回0
     */
    public int delete(List<Integer> ids) {
        //1.首先删除生产情况实体表中的记录，如果删除成功才能删除标签表中的记录
        try {
            for (Integer id : ids) {
                newCleanProductDao.delete(id);
            }
            return 1;
        } catch (Exception e) {
            // TODO: handle exception
            return 0;
        }
    }

    /**
     * 查询所属行业对应的标准号
     * @return 所属行业对应的所有标准号
     */

    public List<MetaCleanProduct> getEvaBasis(String class2) {
        return newCleanProductDao.getEvaBasis(class2);
    }

    public List<MetaCleanProduct> getStandardStatus() {
        return newCleanProductDao.getStandardStatus();
    }
}
